<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.3.5 - src/lbitlib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lbitlib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.3.5</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">94</td>
            <td class="headerCovTableEntry">94</td>
            <td class="headerCovTableEntryHi">100.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:17</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lbitlib.c,v 1.30.1.1 2017/04/19 17:20:42 roberto Exp $
<span class="lineNum">       3 </span>            : ** Standard library for bitwise operations
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : #define lbitlib_c
<span class="lineNum">       8 </span>            : #define LUA_LIB
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : #include &quot;lprefix.h&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : 
<span class="lineNum">      13 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      16 </span>            : #include &quot;lualib.h&quot;
<span class="lineNum">      17 </span>            : 
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span>            : #if defined(LUA_COMPAT_BITLIB)          /* { */
<span class="lineNum">      20 </span>            : 
<span class="lineNum">      21 </span>            : 
<span class="lineNum">      22 </span>            : #define pushunsigned(L,n)       lua_pushinteger(L, (lua_Integer)(n))
<span class="lineNum">      23 </span>            : #define checkunsigned(L,i)      ((lua_Unsigned)luaL_checkinteger(L,i))
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            : 
<span class="lineNum">      26 </span>            : /* number of bits to consider in a number */
<span class="lineNum">      27 </span>            : #if !defined(LUA_NBITS)
<span class="lineNum">      28 </span>            : #define LUA_NBITS       32
<span class="lineNum">      29 </span>            : #endif
<span class="lineNum">      30 </span>            : 
<span class="lineNum">      31 </span>            : 
<span class="lineNum">      32 </span>            : /*
<span class="lineNum">      33 </span>            : ** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
<span class="lineNum">      34 </span>            : ** be made in two parts to avoid problems when LUA_NBITS is equal to the
<span class="lineNum">      35 </span>            : ** number of bits in a lua_Unsigned.)
<span class="lineNum">      36 </span>            : */
<span class="lineNum">      37 </span>            : #define ALLONES         (~(((~(lua_Unsigned)0) &lt;&lt; (LUA_NBITS - 1)) &lt;&lt; 1))
<span class="lineNum">      38 </span>            : 
<span class="lineNum">      39 </span>            : 
<span class="lineNum">      40 </span>            : /* macro to trim extra bits */
<span class="lineNum">      41 </span>            : #define trim(x)         ((x) &amp; ALLONES)
<span class="lineNum">      42 </span>            : 
<span class="lineNum">      43 </span>            : 
<span class="lineNum">      44 </span>            : /* builds a number with 'n' ones (1 &lt;= n &lt;= LUA_NBITS) */
<span class="lineNum">      45 </span>            : #define mask(n)         (~((ALLONES &lt;&lt; 1) &lt;&lt; ((n) - 1)))
<span class="lineNum">      46 </span>            : 
<span class="lineNum">      47 </span>            : 
<span class="lineNum">      48 </span>            : 
<span class="lineNum">      49 </span><span class="lineCov">          3 : static lua_Unsigned andaux (lua_State *L) {</span>
<span class="lineNum">      50 </span><span class="lineCov">          3 :   int i, n = lua_gettop(L);</span>
<span class="lineNum">      51 </span><span class="lineCov">          3 :   lua_Unsigned r = ~(lua_Unsigned)0;</span>
<span class="lineNum">      52 </span><span class="lineCov">          8 :   for (i = 1; i &lt;= n; i++)</span>
<span class="lineNum">      53 </span><span class="lineCov">          5 :     r &amp;= checkunsigned(L, i);</span>
<span class="lineNum">      54 </span><span class="lineCov">          3 :   return trim(r);</span>
<span class="lineNum">      55 </span>            : }
<span class="lineNum">      56 </span>            : 
<span class="lineNum">      57 </span>            : 
<span class="lineNum">      58 </span><span class="lineCov">          1 : static int b_and (lua_State *L) {</span>
<span class="lineNum">      59 </span><span class="lineCov">          1 :   lua_Unsigned r = andaux(L);</span>
<span class="lineNum">      60 </span><span class="lineCov">          1 :   pushunsigned(L, r);</span>
<span class="lineNum">      61 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      62 </span>            : }
<span class="lineNum">      63 </span>            : 
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span><span class="lineCov">          2 : static int b_test (lua_State *L) {</span>
<span class="lineNum">      66 </span><span class="lineCov">          2 :   lua_Unsigned r = andaux(L);</span>
<span class="lineNum">      67 </span><span class="lineCov">          2 :   lua_pushboolean(L, r != 0);</span>
<span class="lineNum">      68 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">      69 </span>            : }
<span class="lineNum">      70 </span>            : 
<span class="lineNum">      71 </span>            : 
<span class="lineNum">      72 </span><span class="lineCov">          1 : static int b_or (lua_State *L) {</span>
<span class="lineNum">      73 </span><span class="lineCov">          1 :   int i, n = lua_gettop(L);</span>
<span class="lineNum">      74 </span><span class="lineCov">          1 :   lua_Unsigned r = 0;</span>
<span class="lineNum">      75 </span><span class="lineCov">          4 :   for (i = 1; i &lt;= n; i++)</span>
<span class="lineNum">      76 </span><span class="lineCov">          3 :     r |= checkunsigned(L, i);</span>
<span class="lineNum">      77 </span><span class="lineCov">          1 :   pushunsigned(L, trim(r));</span>
<span class="lineNum">      78 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      79 </span>            : }
<span class="lineNum">      80 </span>            : 
<span class="lineNum">      81 </span>            : 
<span class="lineNum">      82 </span><span class="lineCov">          1 : static int b_xor (lua_State *L) {</span>
<span class="lineNum">      83 </span><span class="lineCov">          1 :   int i, n = lua_gettop(L);</span>
<span class="lineNum">      84 </span><span class="lineCov">          1 :   lua_Unsigned r = 0;</span>
<span class="lineNum">      85 </span><span class="lineCov">          4 :   for (i = 1; i &lt;= n; i++)</span>
<span class="lineNum">      86 </span><span class="lineCov">          3 :     r ^= checkunsigned(L, i);</span>
<span class="lineNum">      87 </span><span class="lineCov">          1 :   pushunsigned(L, trim(r));</span>
<span class="lineNum">      88 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      89 </span>            : }
<span class="lineNum">      90 </span>            : 
<span class="lineNum">      91 </span>            : 
<span class="lineNum">      92 </span><span class="lineCov">          1 : static int b_not (lua_State *L) {</span>
<span class="lineNum">      93 </span><span class="lineCov">          1 :   lua_Unsigned r = ~checkunsigned(L, 1);</span>
<span class="lineNum">      94 </span><span class="lineCov">          1 :   pushunsigned(L, trim(r));</span>
<span class="lineNum">      95 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">      96 </span>            : }
<span class="lineNum">      97 </span>            : 
<span class="lineNum">      98 </span>            : 
<span class="lineNum">      99 </span><span class="lineCov">          3 : static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {</span>
<span class="lineNum">     100 </span><span class="lineCov">          3 :   if (i &lt; 0) {  /* shift right? */</span>
<span class="lineNum">     101 </span><span class="lineCov">          2 :     i = -i;</span>
<span class="lineNum">     102 </span><span class="lineCov">          2 :     r = trim(r);</span>
<span class="lineNum">     103 </span><span class="lineCov">          2 :     if (i &gt;= LUA_NBITS) r = 0;</span>
<span class="lineNum">     104 </span><span class="lineCov">          2 :     else r &gt;&gt;= i;</span>
<span class="lineNum">     105 </span>            :   }
<span class="lineNum">     106 </span>            :   else {  /* shift left */
<span class="lineNum">     107 </span><span class="lineCov">          1 :     if (i &gt;= LUA_NBITS) r = 0;</span>
<span class="lineNum">     108 </span><span class="lineCov">          1 :     else r &lt;&lt;= i;</span>
<span class="lineNum">     109 </span><span class="lineCov">          1 :     r = trim(r);</span>
<span class="lineNum">     110 </span>            :   }
<span class="lineNum">     111 </span><span class="lineCov">          3 :   pushunsigned(L, r);</span>
<span class="lineNum">     112 </span><span class="lineCov">          3 :   return 1;</span>
<span class="lineNum">     113 </span>            : }
<span class="lineNum">     114 </span>            : 
<span class="lineNum">     115 </span>            : 
<span class="lineNum">     116 </span><span class="lineCov">          1 : static int b_lshift (lua_State *L) {</span>
<span class="lineNum">     117 </span><span class="lineCov">          1 :   return b_shift(L, checkunsigned(L, 1), luaL_checkinteger(L, 2));</span>
<span class="lineNum">     118 </span>            : }
<span class="lineNum">     119 </span>            : 
<span class="lineNum">     120 </span>            : 
<span class="lineNum">     121 </span><span class="lineCov">          1 : static int b_rshift (lua_State *L) {</span>
<span class="lineNum">     122 </span><span class="lineCov">          1 :   return b_shift(L, checkunsigned(L, 1), -luaL_checkinteger(L, 2));</span>
<span class="lineNum">     123 </span>            : }
<span class="lineNum">     124 </span>            : 
<span class="lineNum">     125 </span>            : 
<span class="lineNum">     126 </span><span class="lineCov">          3 : static int b_arshift (lua_State *L) {</span>
<span class="lineNum">     127 </span><span class="lineCov">          3 :   lua_Unsigned r = checkunsigned(L, 1);</span>
<span class="lineNum">     128 </span><span class="lineCov">          3 :   lua_Integer i = luaL_checkinteger(L, 2);</span>
<span class="lineNum">     129 </span><span class="lineCov">          3 :   if (i &lt; 0 || !(r &amp; ((lua_Unsigned)1 &lt;&lt; (LUA_NBITS - 1))))</span>
<span class="lineNum">     130 </span><span class="lineCov">          1 :     return b_shift(L, r, -i);</span>
<span class="lineNum">     131 </span>            :   else {  /* arithmetic shift for 'negative' number */
<span class="lineNum">     132 </span><span class="lineCov">          2 :     if (i &gt;= LUA_NBITS) r = ALLONES;</span>
<span class="lineNum">     133 </span>            :     else
<span class="lineNum">     134 </span><span class="lineCov">          2 :       r = trim((r &gt;&gt; i) | ~(trim(~(lua_Unsigned)0) &gt;&gt; i));  /* add signal bit */</span>
<span class="lineNum">     135 </span><span class="lineCov">          2 :     pushunsigned(L, r);</span>
<span class="lineNum">     136 </span><span class="lineCov">          2 :     return 1;</span>
<span class="lineNum">     137 </span>            :   }
<span class="lineNum">     138 </span>            : }
<span class="lineNum">     139 </span>            : 
<span class="lineNum">     140 </span>            : 
<span class="lineNum">     141 </span><span class="lineCov">          2 : static int b_rot (lua_State *L, lua_Integer d) {</span>
<span class="lineNum">     142 </span><span class="lineCov">          2 :   lua_Unsigned r = checkunsigned(L, 1);</span>
<span class="lineNum">     143 </span><span class="lineCov">          2 :   int i = d &amp; (LUA_NBITS - 1);  /* i = d % NBITS */</span>
<span class="lineNum">     144 </span><span class="lineCov">          2 :   r = trim(r);</span>
<span class="lineNum">     145 </span><span class="lineCov">          2 :   if (i != 0)  /* avoid undefined shift of LUA_NBITS when i == 0 */</span>
<span class="lineNum">     146 </span><span class="lineCov">          2 :     r = (r &lt;&lt; i) | (r &gt;&gt; (LUA_NBITS - i));</span>
<span class="lineNum">     147 </span><span class="lineCov">          2 :   pushunsigned(L, trim(r));</span>
<span class="lineNum">     148 </span><span class="lineCov">          2 :   return 1;</span>
<span class="lineNum">     149 </span>            : }
<span class="lineNum">     150 </span>            : 
<span class="lineNum">     151 </span>            : 
<span class="lineNum">     152 </span><span class="lineCov">          1 : static int b_lrot (lua_State *L) {</span>
<span class="lineNum">     153 </span><span class="lineCov">          1 :   return b_rot(L, luaL_checkinteger(L, 2));</span>
<span class="lineNum">     154 </span>            : }
<span class="lineNum">     155 </span>            : 
<span class="lineNum">     156 </span>            : 
<span class="lineNum">     157 </span><span class="lineCov">          1 : static int b_rrot (lua_State *L) {</span>
<span class="lineNum">     158 </span><span class="lineCov">          1 :   return b_rot(L, -luaL_checkinteger(L, 2));</span>
<span class="lineNum">     159 </span>            : }
<span class="lineNum">     160 </span>            : 
<span class="lineNum">     161 </span>            : 
<span class="lineNum">     162 </span>            : /*
<span class="lineNum">     163 </span>            : ** get field and width arguments for field-manipulation functions,
<span class="lineNum">     164 </span>            : ** checking whether they are valid.
<span class="lineNum">     165 </span>            : ** ('luaL_error' called without 'return' to avoid later warnings about
<span class="lineNum">     166 </span>            : ** 'width' being used uninitialized.)
<span class="lineNum">     167 </span>            : */
<span class="lineNum">     168 </span><span class="lineCov">          8 : static int fieldargs (lua_State *L, int farg, int *width) {</span>
<span class="lineNum">     169 </span><span class="lineCov">          8 :   lua_Integer f = luaL_checkinteger(L, farg);</span>
<span class="lineNum">     170 </span><span class="lineCov">          8 :   lua_Integer w = luaL_optinteger(L, farg + 1, 1);</span>
<span class="lineNum">     171 </span><span class="lineCov">          8 :   luaL_argcheck(L, 0 &lt;= f, farg, &quot;field cannot be negative&quot;);</span>
<span class="lineNum">     172 </span><span class="lineCov">          6 :   luaL_argcheck(L, 0 &lt; w, farg + 1, &quot;width must be positive&quot;);</span>
<span class="lineNum">     173 </span><span class="lineCov">          4 :   if (f + w &gt; LUA_NBITS)</span>
<span class="lineNum">     174 </span><span class="lineCov">          2 :     luaL_error(L, &quot;trying to access non-existent bits&quot;);</span>
<span class="lineNum">     175 </span><span class="lineCov">          2 :   *width = (int)w;</span>
<span class="lineNum">     176 </span><span class="lineCov">          2 :   return (int)f;</span>
<span class="lineNum">     177 </span>            : }
<span class="lineNum">     178 </span>            : 
<span class="lineNum">     179 </span>            : 
<span class="lineNum">     180 </span><span class="lineCov">          4 : static int b_extract (lua_State *L) {</span>
<span class="lineNum">     181 </span>            :   int w;
<span class="lineNum">     182 </span><span class="lineCov">          4 :   lua_Unsigned r = trim(checkunsigned(L, 1));</span>
<span class="lineNum">     183 </span><span class="lineCov">          4 :   int f = fieldargs(L, 2, &amp;w);</span>
<span class="lineNum">     184 </span><span class="lineCov">          1 :   r = (r &gt;&gt; f) &amp; mask(w);</span>
<span class="lineNum">     185 </span><span class="lineCov">          1 :   pushunsigned(L, r);</span>
<span class="lineNum">     186 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     187 </span>            : }
<span class="lineNum">     188 </span>            : 
<span class="lineNum">     189 </span>            : 
<span class="lineNum">     190 </span><span class="lineCov">          4 : static int b_replace (lua_State *L) {</span>
<span class="lineNum">     191 </span>            :   int w;
<span class="lineNum">     192 </span><span class="lineCov">          4 :   lua_Unsigned r = trim(checkunsigned(L, 1));</span>
<span class="lineNum">     193 </span><span class="lineCov">          4 :   lua_Unsigned v = trim(checkunsigned(L, 2));</span>
<span class="lineNum">     194 </span><span class="lineCov">          4 :   int f = fieldargs(L, 3, &amp;w);</span>
<span class="lineNum">     195 </span><span class="lineCov">          1 :   lua_Unsigned m = mask(w);</span>
<span class="lineNum">     196 </span><span class="lineCov">          1 :   r = (r &amp; ~(m &lt;&lt; f)) | ((v &amp; m) &lt;&lt; f);</span>
<span class="lineNum">     197 </span><span class="lineCov">          1 :   pushunsigned(L, r);</span>
<span class="lineNum">     198 </span><span class="lineCov">          1 :   return 1;</span>
<span class="lineNum">     199 </span>            : }
<span class="lineNum">     200 </span>            : 
<span class="lineNum">     201 </span>            : 
<span class="lineNum">     202 </span>            : static const luaL_Reg bitlib[] = {
<span class="lineNum">     203 </span>            :   {&quot;arshift&quot;, b_arshift},
<span class="lineNum">     204 </span>            :   {&quot;band&quot;, b_and},
<span class="lineNum">     205 </span>            :   {&quot;bnot&quot;, b_not},
<span class="lineNum">     206 </span>            :   {&quot;bor&quot;, b_or},
<span class="lineNum">     207 </span>            :   {&quot;bxor&quot;, b_xor},
<span class="lineNum">     208 </span>            :   {&quot;btest&quot;, b_test},
<span class="lineNum">     209 </span>            :   {&quot;extract&quot;, b_extract},
<span class="lineNum">     210 </span>            :   {&quot;lrotate&quot;, b_lrot},
<span class="lineNum">     211 </span>            :   {&quot;lshift&quot;, b_lshift},
<span class="lineNum">     212 </span>            :   {&quot;replace&quot;, b_replace},
<span class="lineNum">     213 </span>            :   {&quot;rrotate&quot;, b_rrot},
<span class="lineNum">     214 </span>            :   {&quot;rshift&quot;, b_rshift},
<span class="lineNum">     215 </span>            :   {NULL, NULL}
<span class="lineNum">     216 </span>            : };
<span class="lineNum">     217 </span>            : 
<span class="lineNum">     218 </span>            : 
<span class="lineNum">     219 </span>            : 
<span class="lineNum">     220 </span><span class="lineCov">         87 : LUAMOD_API int luaopen_bit32 (lua_State *L) {</span>
<span class="lineNum">     221 </span><span class="lineCov">         87 :   luaL_newlib(L, bitlib);</span>
<span class="lineNum">     222 </span><span class="lineCov">         87 :   return 1;</span>
<span class="lineNum">     223 </span>            : }
<span class="lineNum">     224 </span>            : 
<span class="lineNum">     225 </span>            : 
<span class="lineNum">     226 </span>            : #else                                   /* }{ */
<span class="lineNum">     227 </span>            : 
<span class="lineNum">     228 </span>            : 
<span class="lineNum">     229 </span>            : LUAMOD_API int luaopen_bit32 (lua_State *L) {
<span class="lineNum">     230 </span>            :   return luaL_error(L, &quot;library 'bit32' has been deprecated&quot;);
<span class="lineNum">     231 </span>            : }
<span class="lineNum">     232 </span>            : 
<span class="lineNum">     233 </span>            : #endif                                  /* } */
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
